package com.li.service.impl;

import com.li.domain.UploadFile;
import com.li.mapper.UploadFileMapper;
import com.li.service.SystemAddressService;
import com.li.utlis.FilePathUtil;
import com.li.utlis.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.domain.EmployeeRole;
import com.li.mapper.EmployeeRoleMapper;
import com.li.service.EmployeeRoleService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
public class EmployeeRoleServiceImpl extends ServiceImpl<EmployeeRoleMapper, EmployeeRole> implements EmployeeRoleService{

    @Autowired
    EmployeeRoleMapper employeeRoleMapper;

    @Autowired
    UploadFileMapper uploadFileMapper;

    @Autowired
    SystemAddressService systemAddressService;

    /**
     * 上传头像
     * @param uploadFile
     * @param req
     * @return
     */
    @Transactional
    @Override
    public String uploadAvatar(MultipartFile uploadFile, HttpServletRequest req) {
        /*
        上传文件 并 将数据上传文件记录表
         */
        //找到要上传的地址
        String path= FilePathUtil.getSystemAddressServicePath(systemAddressService, "employee-avatar");
        //完善地址 加"/"
        path= FilePathUtil.pathLast(path);
        //把文件上传
        UploadFile fileData = FileUploadUtil.uploadPahtFileAndUploadFile(uploadFile, path, req);
        //添加到文件记录表中
        uploadFileMapper.insert(fileData);
        //将访问地址返回
        return fileData.getPath();
    }
}
